<template>
	<view class="couponList">
		<view class="item" v-for="(item, index) in myCouponList" :key="index">
			<view class="left">
				<view class="price">￥<text>{{item.face_value}}</text></view>
				<view class="priceName">{{item.type==1?'新人券':'满减券'}}</view>
			</view>
			<view class="right">
				<view class="title">{{item.title}}</view>
				<view class="desc">满{{item.full}}可用</view>
				<view class="time">有效期至：{{item.expire_time}}</view>
			</view>
			<button @click="useCoupon" :data-couponid="item.id" :data-couponprice="item.face_value">立即使用</button>
		</view>
		<view class="hint">没有更多券了 |<text @click="openCouponList()">\t去领券中心领取</text></view>
	</view>
</template>

<script>
	import {dns} from '@/pages/dns.js'
	export default {
		data(){
			return {
				price: '',
				myCouponList: []
			}
		},
		onLoad(e) {
			this.price = e.price;
			this.getMyCouponList()
		},
		methods:{
			getMyCouponList(){//获取我的优惠券列表
			var token = uni.getStorageSync("token");
				uni.request({
					method: 'GET',
					data: {
						page: 1,
						limit: 100,
						status: 1
					},
					header:{
						token: token
					},
					url: dns + 'Coupon/my',
					success: (res)=>{
						console.log(res);
						if(res.data.status == 1){
							var usableCoupon = [];
							res.data.data.forEach((value, index)=>{
								var val = parseInt(value.full);
								var price = parseInt(this.price);
								if(val<price){
									usableCoupon.push(value);
								}
							})
							this.myCouponList = usableCoupon;
						}
						if(res.data.status == -1){
							uni.showToast({
								icon: 'none',
								title: res.data.msg,
								duration: 1500
							})
						}
					}
				})
			},
			openCouponList(){//进入优惠券中心
				uni.navigateTo({
					url: '/pages/index/coupon/index'
				})
			},
			useCoupon(e){//使用优惠券
				let coupon = e.currentTarget.dataset.couponid;
				let coupon_price = e.currentTarget.dataset.couponprice;
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1];   //当前页面
				var prevPage = pages[pages.length - 2];  //上一个页面
				//直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
				prevPage.coupon = coupon;
				prevPage.coupon_price = coupon_price;
				uni.navigateBack({
					delta:1
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
		background-color: #f6f6f6;
	}
	.couponList{
		padding: 30upx 20upx;
		background-color: #f6f6f6;
		.item{
			position: relative;
			width: 100%;
			height: 180upx;
			background-color: #fff;
			border-radius: 10upx;
			display: flex;
			margin-bottom: 20upx;
			.left{
				color: #ed702d;
				width: 30%;
				height: 100%;
				padding-left: 40upx;
				padding-top: 32upx;
				.price{
					font-size: 26upx;
					text{
						font-size: 52upx;
					}
				}
				.priceName{
					font-size: 26upx;
					line-height: 20upx;
				}
			}
			.right{
				color: #7c7c7c;
				width: 60%;
				height: 100%;
				padding-top: 30upx;
				.title{
					font-size: 30upx;
					margin-bottom: 4upx;
					color: #000;
				}
				.time{
					font-size: 24upx;
				}
				.desc{
					font-size: 24upx;
					margin-bottom: 4upx;
				}
			}
			button{
				position: absolute;
				right: 20upx;
				top: 60upx;
				width: 120upx;
				line-height: 52upx;
				height: 52upx;
				background-color: #ed702d;
				border-radius: 26upx;
				color: #fff;
				font-size: 24upx;
				box-shadow: 0px 5px 10upx rgba(237, 112, 45,.2);
			}
		}
		.hint{
			text-align: center;
			font-size: 24upx;
			color: #7f7f7f;
			text{
				color: #ed702d;
			}
		}
	}
</style>
